package com.sk.waste.app.activitys

import android.animation.Animator
import android.animation.ObjectAnimator
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.lc.smc.lib.annotations.LCBindView
import com.lc.smc.lib.utils.LCLogger
import com.sk.waste.app.bases.BaseActiivity
import com.sk.waste.app.R
import com.sk.waste.app.config.NetWorkConfig
import com.sk.waste.app.utils.PermissionUtil

class StartActivity : BaseActiivity() {

    @LCBindView(R.id.ac_start_img)
    private lateinit var imageView: ImageView

    @LCBindView(R.id.ac_start_time)
    private lateinit var ttvTime: TextView

    private var countdown = 5
    private var handlerCountdown: Handler? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_start)
        super.onCreate(savedInstanceState)
        setTitleColorLight(false)
        loadImg()
//        startAnimal()
        startCountdown()
    }

    private fun loadImg()
    {
        Glide.with(mContext).load(NetWorkConfig.NET_URL_IMAG_START).transform(CenterCrop(), RoundedCorners(20)).placeholder(R.mipmap.img_start).error(R.mipmap.img_start).into(imageView);
    }

    private fun startAnimal() {
        val animal = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f)
        animal.duration = 2000
        animal.start()
        animal.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator) {
            }

            override fun onAnimationEnd(p0: Animator) {
                toMain()
            }

            override fun onAnimationCancel(p0: Animator) {
            }

            override fun onAnimationRepeat(p0: Animator) {
            }

        })
    }

    /**
     *  @time 2024/5/11  18:16
     *  @author Im_jingwei
     *  @desc 倒计时
     */
    private fun startCountdown() {
        handlerCountdown = Handler(Looper.getMainLooper())
        handlerCountdown?.postDelayed(handRunnableReped, 1000)
    }

    private val handRunnableReped = object : Runnable {
        override fun run() {
            countdown--
            runOnUiThread { ttvTime.text = countdown.toString() }
            if (countdown > 0) {
                handlerCountdown?.postDelayed(this, 1000)
            } else {
                toMain()
            }
        }
    }

    private fun toMain() {
//        val intent = Intent(mContext, MainActivity::class.java)
        val intent = Intent(mContext, LoginActivity::class.java)
        startActivity(intent)
        finish()
    }

}